package com.example.peanutsubway.web;

import com.alibaba.fastjson.JSONObject;
import com.example.peanutsubway.annotation.ResponseTypeAnnotation;
import com.example.peanutsubway.entity.BookInfo;
import com.example.peanutsubway.entity.BookType;
import com.example.peanutsubway.service.BookService;
import com.example.peanutsubway.service.impl.BookServiceImpl;
import com.example.peanutsubway.util.PageInfo;

import javax.servlet.annotation.WebServlet;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/10/12-11:18
 * @Since:jdk1.8
 * @Description:
 */
@WebServlet("/book.do")
public class BookServlet extends BaseServlet<BookInfo> {
    private BookService bookService = new BookServiceImpl();

    @ResponseTypeAnnotation
    public String addBook(BookInfo bookInfo){
        return JSONObject.toJSONString(bookService.addBook(bookInfo));
    }

    @ResponseTypeAnnotation
    public String findAllBook(BookInfo bookInfo) {
        String nowPage = request.getParameter("page");
        String pageNum = request.getParameter("pageSize");
        PageInfo pageInfo = bookService.findAllBook(bookInfo, nowPage, pageNum);
        return JSONObject.toJSONString(pageInfo);

    }

    @ResponseTypeAnnotation
    public String findAllBookParentType(BookType bookType) {
        return JSONObject.toJSONString(bookService.findAllBookParentType());
    }

    @ResponseTypeAnnotation
    public String findAllBookSonType() {
        String booktypeid = request.getParameter("booktypeid");
        return JSONObject.toJSONString(bookService.findAllBookSonType(Integer.valueOf(booktypeid)));
    }

    @ResponseTypeAnnotation
    public String update(BookInfo bookInfo) {
        if (bookService.updateBookInfo(bookInfo)) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }


}
